// pages/shopHome/showHome.js
import request from '../../utils/request'
import api from "../../config/api";
import {
  shareOptions
} from '../../utils/access';
import {
  getBarAndTitleHeight,
} from "../../utils/utils";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    // 列表数据
    list: [],
    // 展示空状态
    showEmpty: false,
    // 搜索内容
    searchValue: '',
    // 是否是搜索
    isSearch: false,
    // 信号栏高度
    ...getBarAndTitleHeight(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ...getBarAndTitleHeight()
    })
    this.getList(1)
  },
  // 获取商品列表
  async getList(page) {
    let {
      isSearch,
      searchValue
    } = this.data
    let url
    let params = {
      status: 5,
      page,
      page_size: 10,
    }
    if (isSearch) {
      url = api.searchShopList
      params.keyword = searchValue
    } else {
      url = api.getShopList
    }
    let res = await request.post(url, params, {
      loading: false
    })
    if (res.httpStatus === 200) {
      this.setData({
        list: page === 1 ? res?.data?.spus : [
          ...this.data.list,
          ...res?.data?.spus,
        ],
        total: res?.data?.total_num / 10,
      })
    }
    if (isSearch) {
      this.setData({
        showEmpty: this.data.list.length === 0
      })
    } else {
      this.setData({
        showEmpty: false
      })
    }
    wx.stopPullDownRefresh()
  },
  // 搜索值改变
  searchInput(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },
  // 搜索数据
  async search() {
    if (this.data.searchValue === '') {
      this.setData({
        isSearch: false,
        page: 1
      })
    } else {
      this.setData({
        isSearch: true,
        page: 1
      })
    }
    this.getList(1)
  },
  // 重置
  refresh() {
    this.setData({
      searchValue: '',
      page: 1,
      isSearch: false
    })
    this.getList(1)
  },
  // 跳转到详情
  jumpDetail(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    });
  },
  // 跳转到订单
  jumpOrder() {
    wx.navigateTo({
      url: 'plugin-private://wx34345ae5855f892d/pages/orderList/orderList',
    });
  },
  // 跳转到购物车
  jumpBag() {
    wx.navigateTo({
      url: 'plugin-private://wx34345ae5855f892d/pages/shoppingCart/shoppingCart',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isSearch && this.data.searchValue === '') {
      this.setData({
        isSearch: false
      })
    }
    this.setData({
      page: 1
    })
    this.getList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let {
      page,
      total
    } = this.data
    if (page < total) {
      page++
      this.setData({
        page
      })
      this.getList(page)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return shareOptions('default', e)
  }
})